package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.Address;
import com.hmall.common.pojo.Item;
import com.hmall.feignclient.AddressFeignClient;
import com.hmall.feignclient.ItemFeignClient;
import com.hmall.order.context.BaseContext;
import com.hmall.order.mapper.OrderDetailMapper;
import com.hmall.order.mapper.OrderLogisticsMapper;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.service.IOrderService;
import com.hmall.order.vo.OrderVO;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.UUID;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private ItemFeignClient itemFeignClient;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;
    @Autowired
    private OrderLogisticsMapper orderLogisticsMapper;
    @Autowired
    private AddressFeignClient addressFeignClient;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    @GlobalTransactional
    public void submitOrder(OrderVO orderVO) {
        Order order=new Order();
        Item item = itemFeignClient.findItemById(orderVO.getItemId());
        //商品单价
        Double price = item.getPrice();
        //商品件数
        Integer num = orderVO.getNum();
        //操作订单表
        order.setTotalFee(num*price);
        order.setPaymentType(orderVO.getPaymentType());
        order.setUserId(BaseContext.getCurrentId());
        order.setStatus(1);
        order.setCreateTime(new Date());
        //payTime
        order.setUpdateTime(new Date());
        //插入订单表
        orderMapper.insert(order);
        //操作订单详情表
        OrderDetail orderDetail=new OrderDetail();
        orderDetail.setOrderId(order.getId());
        orderDetail.setItemId(item.getId());
        orderDetail.setNum(orderVO.getNum());
        orderDetail.setName(item.getName());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setImage(item.getImage());
        orderDetail.setCreateTime(new Date());
        orderDetail.setUpdateTime(new Date());
        //
        orderDetailMapper.insert(orderDetail);
        //操作路由信息数据，需要调用户服务
        Address address = addressFeignClient.findAddressById(orderVO.getAddressId());
        OrderLogistics orderLogistics=new OrderLogistics();
        orderLogistics.setOrderId(order.getId());
        orderLogistics.setLogisticsNumber(UUID.randomUUID().toString());
        orderLogistics.setLogisticsCompany("顺丰");
        orderLogistics.setContact(address.getContact());
        orderLogistics.setMobile(address.getMobile());
        orderLogistics.setProvince(address.getProvince());
        orderLogistics.setCity(address.getCity());
        orderLogistics.setTown(address.getTown());
        orderLogistics.setStreet(address.getStreet());
        orderLogistics.setCreateTime(new Date());
        orderLogistics.setUpdateTime(new Date());
        //
        orderLogisticsMapper.insert(orderLogistics);
        //item表减库存
        Item itemTemp=Item.builder().id(item.getId()).sold((item.getSold()-orderVO.getNum())).build();
        itemFeignClient.updateItemById(itemTemp);
        //TODO 完成延迟队列,发送延迟消息，若15分钟内未支付，则取消订单
        String exchangeName="order.direct.exchange";
        String routingKey="order.ttl";
        //发送延迟消息,订单ID
        rabbitTemplate.convertAndSend(exchangeName,routingKey,order.getId());
        //用户表先不减余额
    }
}
